package org.ruwang.springai_hello.config;

import org.ruwang.springai_hello.constants.SystemConstants;
import org.ruwang.springai_hello.tools.CourseTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringAiConfig {

    @Bean
    public ChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }

    @Bean
    public ChatClient chatClient(OllamaChatModel ollamaChatModel,ChatMemory chatMemory){
        return ChatClient.builder(ollamaChatModel)
                .defaultSystem(SystemConstants.System_Prompt)
                .defaultAdvisors(new SimpleLoggerAdvisor())//日志
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))//会话记忆
                .build();
    }


    @Bean
    public ChatClient gameClient(OpenAiChatModel openAiChatModel,ChatMemory chatMemory){
        return ChatClient.builder(openAiChatModel)
                .defaultSystem(SystemConstants.Game_Prompt)
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))
                .build();
    }
    
    @Bean
    public ChatClient serviceClient(OpenAiChatModel chatModel,
                                    ChatMemory chatMemory,
                                    CourseTools courseTools){
        return ChatClient.builder(chatModel)
                .defaultSystem(SystemConstants.CUSTOMER_SERVICE_SYSTEM)
                .defaultAdvisors(new SimpleLoggerAdvisor())
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory))
                .defaultTools(courseTools)
                .build();
    }
}
